package com.qfedu.repo;

import com.qfedu.repo.bean.Manager;
import com.qfedu.repo.db.DBUtils;
import com.qfedu.repo.db.PstCallback;
import com.sun.xml.internal.bind.v2.TODO;
import sun.util.resources.cldr.to.CalendarData_to_TO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author a1275872671
 * @version v1.0
 * @project QfeduJava2401
 * @package com.qfedu.repo
 * @company
 * @date 2024/5/8 11:20
 */
public class Main {
    public static void main(String[] args) {
        queryTest();
    }

    public static void queryTest() {
        //1.sql语句
        String sql = "select id,account,password,phone,level,remark from manager";
        //2、执行查询操作
        ResultSet result = DBUtils.query(sql, new PstCallback() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                //在此处编写，对pst中的？进行数值设置的语句
                //因为17行的sql中不包含？，因此不需要调用pst.setXXX方法
            }
        });

        //3、处理数据
        List<Manager> list = new ArrayList<>();
        try {
            while (result.next()) {
                int id = result.getInt("id");
                //TODO
                //FIXME
                String account = result.getString("account");
                String pwd = result.getString("password");
                String phone = result.getString("phone");
                String level = result.getString("level");
                String remark = result.getString("remark");

                Manager manager = new Manager(id, account, pwd, phone, level, remark);
                list.add(manager);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("获取到的数据的条数:" + list.size());

        //4、关闭链接
        try {
            if (result != null) {
                result.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        DBUtils.close();
    }

    public static void updateTest() {
        //1、sql语句
        String sql = "insert into manager(account,password,phone,level,remark) values(?,?,?,?,?)";
        //2、调用执行
        Manager admin = new Manager();
        admin.setAccount("admin");
        admin.setPassword("11111111");
        admin.setPhone("13167582311");
        admin.setLevel("level2");
        admin.setRemark("二级管理员");

        //匿名内部类充当回调函数
        int result = DBUtils.executeUpdate(sql, new PstCallback() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                try {
                    pst.setString(1, admin.getAccount());
                    pst.setString(2, admin.getPassword());
                    pst.setString(3, admin.getPhone());
                    pst.setString(4, admin.getLevel());
                    pst.setString(5, admin.getRemark());
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        System.out.println(result);

        //3、关闭数据库
        DBUtils.close();
    }
}
